package cz.spock.cms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cz.spock.cms.entity.Node;
import cz.spock.cms.entity.content.Field;
import cz.spock.core.service.BaseService;

@Service
public class FieldService extends BaseService {
    
    @Autowired
    private NodeService nService;
    
    /**
     * creates Field object, label can be null, value must be set
     * 
     * @param <T>
     * @param label
     * @param value
     * @return
     */
    public <T> Field<T> createField(String label, T value, Node node) {
        return this.createField(label, value, node.getId());
    }

    public <T> Field<T> createField(String label, T value, Long id) {
        Field<T> f = new Field<T>();
        if(label == null) {
            label = "";
        }
        f.setLabel(label);
        if(value == null) {
            // TODO exception
            System.out.println("field value is null!");
        }
        f.setValue(value);
        f.setId(dao.put(f).getId());
        nService.addContentField(id, f.getId());
        return f;
    }

    
    public Field getFieldById(Long id) {
        if(id == null) return null;
        return dao.find(Field.class, id);
    }

}
